import angular from 'angular' 
import OrdersService from './orders.service' 

class ShopsService {
    constructor($http, ConfigService, OrdersService) {
        this.$http = $http;
        this.host = ConfigService.apiServerUrl;
        this.OrdersService = OrdersService;
    }

    getShops() {
        return this.$http.get(this.host + '/admin/shops')
            .then(result => {
                return result.data.data;
            });
    }

    getTimetable(shopID, date) {
        var url = `${this.host}/admin/shops/${shopID}?date=${date.toString('yyyy-MM-dd')}`;
        return this.$http.get(url)
            .then(result => {
                let timeTable = result.data.data;

                timeTable.rooms.forEach(room => {
                    if (room && room.booktimes) {
                        room.booktimes.forEach(time => {
                            this .OrdersService.makeOrderInfoFriendly(time.order);
                        });
                    }
                });

                return timeTable;
            });
    }

    getPeriods(shopID, date) {
        return this.getTimetable(shopID, date).then(
            timeTable => {
                let rooms = timeTable.rooms.map(
                    room => { 
                        let friendlyRoom = this._makeRoomFrinedly(room);
                        return friendlyRoom;

                         //return {
                             //name: friendlyRoom.name,
                             //id: friendlyRoom.id,
                             //periods: friendlyRoom.periods,
                             //disabled: friendlyRoom.disabled
                         //}
                    }
                );

                return {
                    timeTable,
                    rooms
                };
            },
            err => {
                console.erro(err);
            }
        );
    }

    getGuides(shopID, date) {
        return this.$http.get(this.host + '/admin/users')
            .then(result => {
                return result.data.data;
            })
            .catch(err => { console.error(err) });
    }

    getManagers(shopID, date) {
        return this.$http.get(this.host + '/admin/users')
            .then(result => {
                if (result.data.data) {
                    return result.data.data.filter(user => user.role === 'manager');
                }else {
                    return [];
                }
            })
            .catch(err => { console.error(err) });
    }

    updateRoom(shopID, room, date) {
        let data = {
            shopId: shopID,
            date: date.toString('yyyy-MM-dd'),
            roomId: room.id,
            periods: room.periods
        };

        if (room.priceType === 'totalPrice') {
            data.totalPrice = room.tmpPrice;
        }else {
            data.price = room.tmpPrice;
        }

        return this.$http.post(this.host + '/admin/shops/shop', data);
    }

    _makeRoomFrinedly(room) {
        let disabled = false;
        let priceType = !room.totalPrice ? 'pricePerPerson' : 'totalPrice';
        let periods = room.booktimes.map(
            time => {
                if (!!time.order || !time.available) {
                    disabled = true;
                }

                return {
                    begin: time.range.split('-')[0],
                    end: time.range.split('-')[1],
                    disabled: disabled
                }

            }
        );

        return Object.assign({}, room, { 
            periods,
            disabled,
            priceType,
            tmpPrice : +(room.totalPrice || room.price)
        });
    }
}

ShopsService.$inject = ['$http', 'configService', 'ordersService'];

export default angular.module('services.shops', [OrdersService])
    .service('shopsService', ShopsService)
    .name;
